<?php
namespace Admin\Controller;
use Think\Controller;
use Org\SequenceNumber;
use OSS\OssClient;
use OSS\Core\OssException;

class IndexController extends Controller {

    public function index(){

        if(session('?user_info')){
            $user_info = json_encode(session('user_info'));
            $this->assign('user_info',$user_info);

            $this->display();
        }else{
            redirect(C('ADMIN_PATH').'/index/login');
        }
    }

    public function login(){

        $this->display();
    }

    public function loginz(){

        $account = I('post.user_name');
        $password = getPwdEncodeString(I('post.password'));

        $user = M('Manage');
        $user_info = $user->where("account = '$account'")->find();

        if($user_info === null){

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "账号不存在！";

        }else if($user_info['password'] != $password){

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "密码错误！";

        }else if($user_info['status'] == 0){

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "该账号已被禁止登录！";

        }else{

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";

            session('user_info', $user_info);

            $data['last_login'] = time();
            $userid = $user_info['id'];

            $user->where('id='.$userid)->save($data);

        }

        $this->ajaxReturn($ajaxReturn);

    }

    public function logout(){

        session('user_info',null);
        redirect(C('ADMIN_PATH').'/index/login');

    }

    public function menu(){

        $banner      = ["menu_name" => "广告主管理", "url" =>"ad-manage", "icon" => "glyphicon-file"];

        $channel     = ["menu_name" => "渠道管理", "url" =>"channel-manage", "icon" => "glyphicon-file"];

        $gameBanner  = ["menu_name" => "游戏广告管理", "url" =>"game-ad", "icon" => "glyphicon-file"];

        $gameUser    = ["menu_name" => "游戏用户管理", "url" =>"game-userAd", "icon" => "glyphicon-file"];
            // $gameUser['son'][] = ["menu_name" => "游戏用户", "url" => "game-userAd"];
            //$gameUser['son'][] = ["menu_name" => "渠道", "url" => "game-userCh"];
            
        $recharge    = ["menu_name" => "充值统计", "url" =>"", "icon" => "glyphicon-list-alt"];
            $recharge['son'][] = ["menu_name" => "广告主统计", "url" => "recharge-adCharge"];
            $recharge['son'][] = ["menu_name" => "渠道统计", "url" => "recharge-chCharget"];

        $orders    = ["menu_name" => "订单管理", "url" =>"", "icon" => "glyphicon-list-alt"];
            $orders['son'][] = ["menu_name" => "广告主订单", "url" => "recharge-adList"];
            $orders['son'][] = ["menu_name" => "渠道订单", "url" => "recharge-chList"];

        $drawMoney   = ["menu_name" => "提现管理", "url" =>"", "icon" => "glyphicon-list-alt"];
            $drawMoney['son'][] = ["menu_name" => "广告主提现", "url" => "drawMoney-adList"];
            $drawMoney['son'][] = ["menu_name" => "渠道提现", "url" => "drawMoney-chList"];
        
        $business    = ["menu_name" => "商务管理", "url" =>"business-list", "icon" => "glyphicon-file"];
        $medium    = ["menu_name" => "媒介管理", "url" =>"medium-list", "icon" => "glyphicon-file"];


        $feedback    = ["menu_name" => "反馈", "url" =>"feedback-list", "icon" => "glyphicon-file"];

        $partener    = ["menu_name" => "合作客户", "url" =>"partener-list", "icon" => "glyphicon-file"];

        $design    = ["menu_name" => "定制推广", "url" =>"design-list", "icon" => "glyphicon-file"];

        $numberManage    = ["menu_name" => "公众号管理", "url" =>"numberManage-list", "icon" => "glyphicon-file"];

        $numberPrice    = ["menu_name" => "公众号报价", "url" =>"numberPrice-list", "icon" => "glyphicon-file"];

        $numberCategory    = ["menu_name" => "公众号类别", "url" =>"numberCategory-list", "icon" => "glyphicon-file"];

        $topBanner    = ["menu_name" => "Banner管理", "url" =>"topBanner-list", "icon" => "glyphicon-file"];

        $hotSearch    = ["menu_name" => "热门搜索", "url" =>"hotSearch-list", "icon" => "glyphicon-file"];

        $caseShow    = ["menu_name" => "案例展示", "url" =>"caseShow-list", "icon" => "glyphicon-file"];

        $orderManager   = ["menu_name" => "下单管理", "url" =>"orderManager-list", "icon" => "glyphicon-file"];

        $gameCategory   = ["menu_name" => "游戏分类", "url" =>"gameCategory-list", "icon" => "glyphicon-file"];

        $announcement   = ["menu_name" => "公告", "url" =>"announcement-list", "icon" => "glyphicon-file"];

        $gameUserWei   = ["menu_name" => "威信游戏用户", "url" =>"gameUserWei-adlist", "icon" => "glyphicon-file"];

        $weiXinGame    = ["menu_name" => "威信游戏管理", "url" =>"", "icon" => "glyphicon-list-alt"];
            $weiXinGame['son'][] = ["menu_name" => "威信最新游戏管理", "url" =>"newGame-list"];
            $weiXinGame['son'][] = ["menu_name" => "威信最热游戏管理", "url" =>"hotGame-list"];
            $weiXinGame['son'][] = ["menu_name" => "威信游戏榜单管理", "url" =>"billBoard-list"];

        // $newGame   = ["menu_name" => "威信最新游戏管理", "url" =>"newGame-list", "icon" => "glyphicon-file"];

        // $hotGame   = ["menu_name" => "威信最热游戏管理", "url" =>"hotGame-list", "icon" => "glyphicon-file"];

        // $billBoard   = ["menu_name" => "威信游戏榜单管理", "url" =>"billBoard-list", "icon" => "glyphicon-file"];

        $menu_tree = [$banner, $channel, $gameBanner, $gameUser, $recharge, $orders, $drawMoney, $business, $medium, $feedback ,$partener, $design, $numberManage, $numberPrice, $numberCategory, $topBanner, $hotSearch, $caseShow, $orderManager, $gameCategory ,$announcement, $gameUserWei, $weiXinGame];

        $ajaxReturn['code'] 		= 1;
        $ajaxReturn['msg'] 			= "SUCCESS";
        $ajaxReturn['data']			= $menu_tree;

        $this->ajaxReturn($ajaxReturn);
    }

     public function uploadfile(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =    3145728 ;// 设置附件上传大小
        $upload->exts      =    array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =    './uploads/'; // 设置附件上传根目录
        $upload->saveName  =    'dd2char';
        $upload->autoSub   =    false;
        // 上传文件
        $info   =   $upload->uploadOne($_FILES['Filedata']);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
            $ajaxReturn['code']         = 2;
            $ajaxReturn['msg']          = $upload->getError();
        }else{// 上传成功 获取上传文件信息
            $this->uploadOss($info['savename']);

            $ajaxReturn['code']         = 1;
            $ajaxReturn['filename']     = $info['savename'];


        }
        $this->ajaxReturn($ajaxReturn);
    }

    private function uploadOss($filename){
        try {
            $ossClient = new OssClient(C('OSS_ACCESS_KEYID'), C('OSS_ACCESS_KEY_SECRET'), C('OSS_ENDPOINT'));
        } catch (OssException $e) {
            print $e->getMessage(); 
        }

        $object = $filename;
        $filePath = './uploads/'.$filename;
        //echo $filePath;die;
        try{
            $ossClient->uploadFile(C('OSS_BUCKET'), $object, $filePath);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
    }

    public function resetPassword(){

        $this->isAjaxLogin();//检查是否登录

        $oldpwd	= getPwdEncodeString(I('post.oldpwd'));
        $newpwd  = getPwdEncodeString(I('post.newpwd'));

        $userinfo = session('user_info');

        if($oldpwd != $userinfo['password']){

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg']  = '旧密码错误!';
        }else{

            $Model = M('Manage');

            $userid = $userinfo['id'];

            $data['password'] = $newpwd;

            $Model->where('id='.$userid)->save($data);

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg']  = '设置成功!';
        }

        $this->ajaxReturn($ajaxReturn);
    }
}